package com.sgz.test.bean.copybean;

import com.sgz.bean.FromBean;
import com.sgz.bean.ToBean;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-8-15
 * Time: 上午10:40
 * To change this template use File | Settings | File Templates.
 */
public class CopyBeanUtils extends MyTransformer {
    public static void copyBean(Object orig, Object dest, String[] origProps, String[] destProps) throws Exception {

        if ((origProps != null && destProps != null) && origProps.length != destProps.length) {
            throw new Exception("属性长度不相等");
        }
        Field[] destFields = dest.getClass().getDeclaredFields();
        Field[] origFields = orig.getClass().getDeclaredFields();
        Set<String> fieldSet = intersect(origFields,destFields);
        Iterator<String> it = fieldSet.iterator();
        while (it.hasNext()) {
            String fieldName = it.next();
            Object value = PropertyUtils.getProperty(orig, fieldName);
            PropertyUtils.setProperty(dest, fieldName, value);
        }
        if (origProps != null) {
            for (int i = 0; i < destProps.length; i++) {
                Object value = PropertyUtils.getProperty(orig, origProps[i]);
                PropertyUtils.setProperty(dest,destProps[i], value);
            }
        }


        /*List<String> superFieldList = new ArrayList<String>();
        Class<?> superClass = Object.class.getSuperclass();
        while(!superClass.getSimpleName().equals("Object")){
            Field[] superFields = superClass.getDeclaredFields();
            for (Field superField : superFields) {
                superFieldList.add(superField.getName());
            }

        }
*/
    }

    public void copyBeanNoCheck(Object orig, Object dest, String[] origProps, String[] destProps){

    }


    public static void copyAll(Object orig, Object dest) throws Exception {
        Field[] origFields = orig.getClass().getDeclaredFields();
        for (Field origField : origFields) {
            String fieldName = origField.getName();
            Object value = PropertyUtils.getProperty(orig,fieldName);
            PropertyUtils.setProperty(dest,fieldName, value);
        }
    }

    public static List<String> toArrayList(String[] array) {
        List<String> list = new ArrayList<String>();
        for (String str : array) {
            list.add(str);
        }
        return list;
    }

    /**
     * 求两个字符串数组的交集
     * @return  两个字符串数组的交集
     */
    public static Set<String> intersect(Field[] origFields, Field[] destFields) {
        Set<String> set = new HashSet<String>();
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        for (Field field : origFields) {
            if (!map.containsKey(field.getName())) {
                map.put(field.getName(), Boolean.FALSE);
            }
        }
        for (Field  destField : destFields) {
            if (map.containsKey(destField.getName())) {
                map.put(destField.getName(), Boolean.TRUE);
            }
        }
        for (Map.Entry<String, Boolean> elem : map.entrySet()) {
            if (elem.getValue().equals(Boolean.TRUE)) {
                set.add(elem.getKey());
            }
        }
        return set;
    }


    public static void main(String[] args) throws Exception {
        OrigBean origBean = new OrigBean(1l, "测试", "property1ceshi", "property2");
        origBean.setDataId(15);
        DestBean destBean = new DestBean();
        CopyBeanUtils.copyBean(origBean, destBean, new String[]{"property1"}, new String[]{"property11"});
        System.out.println("destBean = " + destBean.toString());
    }

}
